package com.ruoyi.web.controller.material;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.material.domain.FoodType;
import com.ruoyi.material.service.IFoodTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 食品类型Controller
 *
 * @author wng
 * @date 2024-08-04
 */
@RestController
@RequestMapping("/material/foodType")
public class FoodTypeController extends BaseController {
    @Resource
    private IFoodTypeService foodTypeService;

    /**
     * 查询食品类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(FoodType foodType) {
        startPage();
        LoginUser loginUser = getLoginUser();
        if (loginUser.getUserId() != 1 && foodType.getDeptId() == null) {
            foodType.setDeptId(loginUser.getDeptId());
        }
        List<FoodType> list = foodTypeService.selectFoodTypeList(foodType);
        return getDataTable(list);
    }

    /**
     * 导出食品类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:export')")
    @Log(title = "食品类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FoodType foodType) {
        List<FoodType> list = foodTypeService.selectFoodTypeList(foodType);
        ExcelUtil<FoodType> util = new ExcelUtil<FoodType>(FoodType.class);
        util.exportExcel(response, list, "食品类型数据");
    }

    /**
     * 获取食品类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(foodTypeService.selectFoodTypeById(id));
    }

    /**
     * 新增食品类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "食品类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FoodType foodType) {
        LoginUser loginUser = getLoginUser();
        foodType.setCreateBy(String.valueOf(loginUser.getUser().getUserId()));
        foodType.setDeptId(loginUser.getDeptId());
        return toAjax(foodTypeService.insertFoodType(foodType));
    }

    /**
     * 修改食品类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "食品类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FoodType foodType) {
        LoginUser loginUser = getLoginUser();
        foodType.setUpdateBy(String.valueOf(loginUser.getUser().getUserId()));
        return toAjax(foodTypeService.updateFoodType(foodType));
    }

    /**
     * 删除食品类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "食品类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(foodTypeService.deleteFoodTypeByIds(ids));
    }
}
